package com.fieldchain.ex.fa100.utils.net.common;

import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.access.MyApplication;
import com.fieldchain.ex.fa100.bean.event.BaseEvent;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.net.tools.NetStatus;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/11
 */
public class SmsCode {
    private static SmsCode smsCode;

    public static SmsCode getInstance() {
        if (null == smsCode) {
            smsCode = new SmsCode();
        }
        return smsCode;
    }

    public void getSms(String phone, String imgCode) {
        if (phone.length() == 0) {
            MyToast.show("请输入手机号");
            stop();
            return;
        }

        if (imgCode.length() == 0) {
            stop();
            MyToast.show("请输入图形验证码");
            return;
        }

        if(!NetStatus.isNetWorkConnected(MyApplication.getAppContext())){
            stop();
            MyToast.show("网络连接失败");
            return;
        }
        HashMap map = new HashMap();
        map.put("phone", phone);
        map.put("imgCode", imgCode);
        map.put("apiName", "");
        HttpRequest.getInstance().getRequest(map, Constant.smsCode, "null",new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {

                MyToast.show(msg);
                if(code!=200)stop();
            }

            @Override
            public void onFaild(Exception e) {
                stop();
            }
        });
    }
    public void stop(){
        EventBus.getDefault().post(new BaseEvent(3));
    }
}
